package com.cgah

import grails.test.*
import com.cgah.Listing
import com.cgah.Customer

class ListingTests extends GrailsUnitTestCase {

    Customer seller;
    Listing theListing;



    protected void setUp() {
        super.setUp();
        mockDomain(Listing);
        mockDomain(Customer);
        seller = new Customer(username: "blabababa", emailAddress: "asdf@asdf.com", createdDate: new Date(), password: "12312312", enabled: true);
/*

	String name;
	Date endDateTime;
	Float startingBidPrice;
	String description;
    Date createdDateTime;
*/

        theListing = new Listing(createdDateTime:new Date(),  description: "asdf", seller:seller, startingBidPrice: 11.0f, name: "test", endDateTime: (new Date())+1);
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testSomething() {

    }

 //Done - L-9: Listing has a nullable field for the winner (Customer) (integration test)
    void testWinnerNullable() {
         Listing c =     theListing;
        assertNull(c.winner);
        assertTrue("good listing", c.validate());


    }

//Done - L-8: Listing name must be less than 64 characters (integration test)

    void testValidListingName() {
        def c =     theListing;
        c.name = null;
        assertFalse("good listing", c.validate());

         String n = "X";
         for (i in 0..63) {
             n+="X"
         }
        c.name = n;
        assertFalse("good listing", c.validate());

        n = "Y";
         for (i in 0..62) {
             n+="X"
         }
        c.name = n;
        assertTrue("good listing", c.validate());

    }

//    Done - L-6: Listing descriptions must be less than 256 characters (integration test)
     void testListingDescLimit() {
         def c = theListing;
        assertNotNull(c.getProperty("description"));

         // 256 is bad
         String d = "X";
         for (i in 0..253) {
             d+="X"
         }
         c.description = d;
         assertFalse("good listing", c.validate());

         // 255 is good
         d = "Y";
        for (i in 0..252) {
             d+="Y"
        }
        c.description = d;
        assertTrue("good listing", c.validate());


     }


//    L-5: Updating a seller's information should not modify any properties of that seller's listings (integration test)
  void testUpdateSellerNotTouchListing() {
      Customer s = theListing.seller;
      s.save(flush:true);
       // never touched listing. otherwise id would have been assigned!
      assertNull(theListing.id);
  }

//    - L-4: Updating a Listing should not update the seller's information (integration test)
  void testListingNotUpdateSeller() {
     // Customer origSeller = seller.clone();
      assertTrue("good listing", theListing.validate());
      theListing.name = "whatever";
      assertNotNull(theListing.save(flush:true));

      // never touched seller. otherwise id would have been assigned!
      assertNull(theListing.seller.id);
  }


    //Done - L-3: Listings are required to have a seller (Customer) (unit test)
    void testRequiredSeller() {
        def l = theListing;
        assertTrue("good listing", l.validate());
        l.seller = null;
         assertFalse("good listing", l.validate());
    }


    //Done - L-1: Listings have the following required fields: name, end date/time, and starting bid price (unit test)
    //Done - L-2: Listings have the following optional fields: description (unit test)
    // Done - B-4: A Listing has a list of Bids for that Listing (integration test)
    void testFieldsExist() {
        def c = theListing;
        assertNotNull(c.getProperty("description"));
        c.description = null;
        assertTrue("good listing", c.validate());
        assertNull(c.getProperty("description"));
        assertNotNull(c);
        assertNull(c.getProperty("bids"));
    }
}
